#include <bits/stdc++.h>
// 2025/01/21
// tag:
// Author: Zhang Muen
using namespace std;

class UF
{
public:
    vector<int> parents;
    int cnt;

    UF(int n)
    {
        cnt = n;
        for (int i = 0; i < n; i++)
            parents.push_back(i);
    };

    int find(int i)
    {
        if (parents[i] != i)
            return parents[i] = find(parents[i]);
        return parents[i];
    }

    void un(int i, int j)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        parents[ri] = rj;
        cnt--;
    }

    int count()
    {
        return cnt;
    }

    bool connected(int p, int q)
    {
        return find(p) == find(q);
    }
};

int n, m, road[100001][3];

bool check(int time)
{
    UF uf(n + 1);
    for (int i = 0; i < m; i++)
    {
        if (road[i][2] > time)
            continue;
        uf.un(road[i][0], road[i][1]);
    }
    return uf.count() == 2;
}

signed main()
{
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> road[i][0] >> road[i][1] >> road[i][2];
    }
    int left = 0, right = 1e9;
    while (left < right)
    {
        int mid = left + (right - left) / 2;
        if (check(mid))
            right = mid;
        else
            left = mid + 1;
    }
    if (check(1000000001))
        cout << left;
    else
        cout << -1;
    return 0;
}